﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKRevitKernel.CurveUtility.LineGroupSuite;

namespace YJKRevitArea.AreaThumbnail
{
	public class FirePreventionThumbnailManager
	{
		public static List<GraphicsStyle> GetAllLineType(Autodesk.Revit.DB.Document doc)
		{
			List<GraphicsStyle> list = new List<GraphicsStyle>();
			foreach (ElementId elementId in FilledRegion.GetValidLineStyleIdsForFilledRegion(doc))
			{
				Element element = doc.GetElement(elementId);
				if (element is GraphicsStyle)
				{
					list.Add(element as GraphicsStyle);
				}
			}
			return list;
		}

		public static List<FirePreventionData> GetAllOldFirePreventionThumbnailData(Autodesk.Revit.DB.Document doc, int eleId)
		{
			List<FirePreventionData> list = new List<FirePreventionData>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc).OfClass(typeof(Autodesk.Revit.DB.View));
			Autodesk.Revit.DB.View view = null;
			foreach (Element element in filteredElementCollector)
			{
				Autodesk.Revit.DB.View view2 = element as Autodesk.Revit.DB.View;
				if (element.Id.IntegerValue == eleId)
				{
					view = view2;
					break;
				}
			}
			if (view == null)
			{
				return null;
			}
			FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(doc, view.Id);
			filteredElementCollector2.OfClass(typeof(SpatialElement));
			List<Area> list2 = new List<Area>();
			foreach (Element element2 in filteredElementCollector2)
			{
				Area area = element2 as Area;
				if (area != null)
				{
					list2.Add(area);
				}
			}
			foreach (Area area2 in list2)
			{
				FirePreventionData firePreventionData = new FirePreventionData();
				List<CurveLoop> zoneBoundaryCurves = FirePreventionThumbnailManager.GetZoneBoundaryCurves(area2);
				List<List<Curve>> list3 = new List<List<Curve>>();
				foreach (CurveLoop curveLoop in zoneBoundaryCurves)
				{
					List<Curve> list4 = new List<Curve>();
					foreach (Curve item in curveLoop)
					{
						list4.Add(item);
					}
					list3.Add(list4);
				}
				firePreventionData.AreaOutline = list3;
				Parameter parameter = area2.GetParameter("区域编号");
				if (parameter != null)
				{
					firePreventionData.AreaNameNum = parameter.AsString();
				}
				parameter = area2.GetParameter("区域前缀");
				if (parameter != null && parameter.HasValue)
				{
					firePreventionData.AreaNamePrefix = parameter.AsString();
				}
				parameter = area2.GetParameter("面积");
				if (parameter != null)
				{
					firePreventionData.Area = parameter.AsValueString();
				}
				parameter = area2.GetParameter("名称");
				if (parameter != null)
				{
					firePreventionData.AreaName = parameter.AsString();
				}
				list.Add(firePreventionData);
			}
			return list;
		}

		public static List<FontFamily> GetAllTypeface()
		{
			List<FontFamily> list = new List<FontFamily>();
			foreach (FontFamily item in new InstalledFontCollection().Families)
			{
				list.Add(item);
			}
			return list;
		}

		public static List<Autodesk.Revit.DB.View> GetAllView(Autodesk.Revit.DB.Document doc)
		{
			List<Autodesk.Revit.DB.View> list = new List<Autodesk.Revit.DB.View>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc).OfClass(typeof(ViewPlan));
			ElementId id = doc.ActiveView.GenLevel.Id;
			foreach (Element element in filteredElementCollector)
			{
				Autodesk.Revit.DB.View view = element as Autodesk.Revit.DB.View;
				if (view.GenLevel != null && view.GenLevel.Id.IntegerValue == id.IntegerValue && (int)view.ViewType == 116)
				{
					list.Insert(0, view);
				}
			}
			return list;
		}

		public static List<FirePreventionUseLine> GetOldGridLocationLine(Autodesk.Revit.DB.Document doc, List<ILineGeter> inputLines)
		{
			List<FirePreventionUseLine> list = new List<FirePreventionUseLine>();
			foreach (List<ILineGeter> list2 in LineGroupManger.GroupLines((from n in inputLines
			where n.UseLine != null
			select n).ToList<ILineGeter>(), 4u, AssistFunc.mmToFeet(200.0)))
			{
				int num = 0;
				if (list2.Count > 1)
				{
					using (List<ILineGeter>.Enumerator enumerator2 = list2.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							if ((enumerator2.Current as FirePreventionUseLine).GridLine)
							{
								num++;
							}
						}
					}
					if (list2.Count != num)
					{
						foreach (ILineGeter lineGeter in list2)
						{
							FirePreventionUseLine firePreventionUseLine = lineGeter as FirePreventionUseLine;
							if (firePreventionUseLine.GridLine)
							{
								Parameter parameter = firePreventionUseLine.Grid.GetParameter("名称");
								if (parameter != null)
								{
									firePreventionUseLine.GridName = parameter.AsString();
									list.Add(firePreventionUseLine);
								}
							}
						}
					}
				}
			}
			return list;
		}

		public static TextNoteType GetTextNodeType(Autodesk.Revit.DB.Document doc, double dTextSize, double dTextWidthScale, string strName)
		{
			ICollection<Element> collection = new FilteredElementCollector(doc).OfClass(typeof(TextNoteType)).ToElements();
			string text = string.Concat(new object[]
			{
				strName,
				"_",
				dTextSize,
				"_",
				dTextWidthScale
			});
			foreach (Element element in collection)
			{
				if (element.Name.Equals(text))
				{
					return element as TextNoteType;
				}
			}
			string text2 = text;
			TextNoteType textNoteType = (collection.First<Element>() as TextNoteType).Duplicate(text2) as TextNoteType;
			textNoteType.get_Parameter(BuiltInParameter.TEXT_FONT).Set(strName);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_SIZE).Set(dTextSize / 304.8);
			textNoteType.get_Parameter(BuiltInParameter.LINE_COLOR).Set(0);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_STYLE_BOLD).Set(0);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_STYLE_ITALIC).Set(0);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_STYLE_UNDERLINE).Set(0);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).Set(dTextWidthScale);
			textNoteType.get_Parameter(BuiltInParameter.LEADER_OFFSET_SHEET).Set(0);
			textNoteType.get_Parameter(BuiltInParameter.TEXT_BACKGROUND).Set(1);
			return textNoteType;
		}

		public static List<List<Curve>> CreateTransform(double dMultiple, List<List<Curve>> lstLstCv)
		{
			List<List<Curve>> list = new List<List<Curve>>();
			Transform identity = Transform.Identity;
			foreach (List<Curve> list2 in lstLstCv)
			{
				List<Curve> list3 = new List<Curve>();
				foreach (Curve curve in list2)
				{
					Curve item = curve.CreateTransformed(identity.ScaleBasis(dMultiple));
					list3.Add(item);
				}
				list.Add(list3);
			}
			return list;
		}

		public static void CreateNewArc(Arc oldarc, XYZ ptNewEnd, XYZ ptNewStart, ref Curve curve)
		{
			XYZ center = oldarc.Center;
			XYZ xyz = ptNewStart.Subtract(center);
			XYZ xyz2 = ptNewEnd.Subtract(center);
			double num = xyz.AngleOnPlaneTo(xyz2, oldarc.Normal);
			XYZ xyz3 = Transform.CreateRotationAtPoint(oldarc.Normal, num * 0.5, center).OfVector(xyz);
			XYZ xyz4 = center.Add(xyz3);
			curve = Arc.Create(ptNewStart, ptNewEnd, xyz4);
		}

		public static void FindFile(string strFilePath)
		{
			if (!File.Exists(strFilePath))
			{
				new FileStream(strFilePath, FileMode.CreateNew).Close();
			}
		}

		private static List<CurveLoop> GetZoneBoundaryCurves(Area area)
		{
			List<CurveLoop> list = new List<CurveLoop>();
			bool flag = true;
			foreach (IList<BoundarySegment> lstBoundSegment in area.GetBoundarySegments(new SpatialElementBoundaryOptions()))
			{
				CurveLoop item = new CurveLoop();
				if (flag)
				{
					try
					{
						item = FirePreventionThumbnailManager.GetAreaCurveLoop(lstBoundSegment);
						flag = false;
						goto IL_49;
					}
					catch
					{
						continue;
					}
				}
				try
				{
					item = FirePreventionThumbnailManager.GetAreaCurveLoop(lstBoundSegment);
				}
				catch
				{
					continue;
				}
				IL_49:
				list.Add(item);
			}
			return list;
		}

		private static CurveLoop GetAreaCurveLoop(IList<BoundarySegment> lstBoundSegment)
		{
			List<Curve> list = new List<Curve>();
			List<List<Curve>> list2 = new List<List<Curve>>();
			foreach (BoundarySegment segment in lstBoundSegment)
			{
				Curve curve = segment.GetCurve();
				list.Add(curve);
			}
			list2.Add(list);
			return CurveLoop.Create(DrawFirePreventionThumbnail.DeleteShortLine(list2)[0]);
		}
	}
}
